#!/usr/bin/perl
$dir = $ARGV[0];
$clonesfn = $ARGV[1];
$patternsfn = $ARGV[2];

# Change directory to that of simulation run
chdir($dir);

# Remove duplicates from the .clones log
my %h=();
open(in, $clonesfn);
open(out, ">$clonesfn.out");
while(<in>){
    chomp;
    @r = split(/\,/);
    if(!defined($h{$r[0]})){
	print out $_."\n";
    }
    $h{$r[0]}=1;
}
close out;
close in;

%h = ();
# Re-read the file and output the line for a clone only once
open(in, "$patternsfn");
open(out, ">$patternsfn.out");
while(<in>){
    chomp;
    #@r = split(/\,/);
    # Speedup with index() and substr()
    $r = substr($_, 0, index($_,','));
    if(!defined($h{$r})){
	print out $_."\n";
    }
    $h{$r}=1;
}
close out;
close in;		

@args = "mv $clonesfn.out $clonesfn";
system(@args);
@args = "mv $patternsfn.out $patternsfn";
system(@args);
